package com.qf.dao;

import com.qf.pojo.User;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author fy
 * @version V1.0
 * @Project day41
 * @Package com.qf.dao
 * @Description:
 * @Date 2022/5/27 12:45
 */
public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDateSource());
    @Override
    public User findByUserName(String userName) {
        String sql = "select * from db_user where username=?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<>(User.class), userName);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addUser(User user) {
        String sql = "insert into db_user (username,password,status) values(?,?,?)";
        try {
            queryRunner.update(sql, user.getUsername(),user.getPassword(),user.getStatus());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
